/*
 * GoldSaveModel.cpp
 *
 *  Created on: 2015-8-13
 *      Author: gaoyu
 */
#include "GoldSaveModel.h"

#define    GOLD_SAVE_KEY   "GoldSaveKeyValue-Gold"
#define    DIAMOND_SAVE_KEY   "DiamondSaveKeyValue-Gold"
#define    DEF_GOLD_NUM    1000
#define    MAX_GOLD_NUM    10000000
#define    DEF_DIAMOND_NUM    0
#define    MAX_DIAMOND_NUM    10000000

static GoldSaveModel *s_SharedGoldSaveModel = nullptr;

GoldSaveModel* GoldSaveModel::getInstance()
{
    if (!s_SharedGoldSaveModel)
    {
    	s_SharedGoldSaveModel = new (std::nothrow) GoldSaveModel();
        CCASSERT(s_SharedGoldSaveModel, "FATAL: Not enough memory");
        s_SharedGoldSaveModel->init();
    }

    return s_SharedGoldSaveModel;
}

GoldSaveModel::GoldSaveModel()
:m_goldNum(0)
,m_diamondNUM(0)
{
}

bool GoldSaveModel::init(void)
{
	m_goldNum = UserDefault::getInstance()->getIntegerForKey(GOLD_SAVE_KEY,-1);
	if(m_goldNum == -1){ m_goldNum = DEF_GOLD_NUM; }
	m_diamondNUM = UserDefault::getInstance()->getIntegerForKey(DIAMOND_SAVE_KEY,-1);
	if(m_diamondNUM == -1){ m_diamondNUM = DEF_DIAMOND_NUM; }
    return true;
}

GoldSaveModel::~GoldSaveModel(void)
{
	s_SharedGoldSaveModel = nullptr;
}

int GoldSaveModel::getGoldNum()
{
    return m_goldNum;
}

void GoldSaveModel::changeGoldNum(int data)
{
    m_goldNum += data;
    if(m_goldNum < 0) m_goldNum = 0;
    if(m_goldNum > MAX_GOLD_NUM) m_goldNum = MAX_GOLD_NUM;
    if(data != 0){
    	UserDefault::getInstance()->setIntegerForKey(GOLD_SAVE_KEY,m_goldNum);
    }
}

int GoldSaveModel::getDiamondNum()
{
    return m_diamondNUM;
}

void GoldSaveModel::changeDiamondNum(int data)
{
	m_diamondNUM += data;
    if(m_diamondNUM < 0) m_diamondNUM = 0;
    if(m_diamondNUM > MAX_DIAMOND_NUM) m_diamondNUM = MAX_GOLD_NUM;
    if(data != 0){
    	UserDefault::getInstance()->setIntegerForKey(DIAMOND_SAVE_KEY,m_diamondNUM);
    }
}
